package com.ztorn.fiscale.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ztorn.fiscale.mapper.FiscaleRefundExceptionMapper;
import com.ztorn.fiscale.mapper.FiscaleCustomsDeclarationMapper;
import com.ztorn.fiscale.mapper.FiscaleTaxInvoiceMapper;
import com.ztorn.fiscale.domain.FiscaleRefundException;
import com.ztorn.fiscale.domain.FiscaleCustomsDeclaration;
import com.ztorn.fiscale.domain.FiscaleTaxInvoice;
import com.ztorn.fiscale.service.IFiscaleRefundExceptionService;

/**
 * 配单异常记录Service业务层处理
 *
 * @author ztorn
 * @date 2025-09-20
 */
@Service
public class FiscaleRefundExceptionServiceImpl implements IFiscaleRefundExceptionService
{
    @Autowired
    private FiscaleRefundExceptionMapper fiscaleRefundExceptionMapper;

    @Autowired
    private FiscaleCustomsDeclarationMapper fiscaleCustomsDeclarationMapper;

    @Autowired
    private FiscaleTaxInvoiceMapper fiscaleTaxInvoiceMapper;

    /**
     * 查询配单异常记录
     *
     * @param exceptionId 配单异常记录ID
     * @return 配单异常记录
     */
    @Override
    public FiscaleRefundException selectFiscaleRefundExceptionById(Long exceptionId)
    {
        FiscaleRefundException exception = fiscaleRefundExceptionMapper.selectFiscaleRefundExceptionById(exceptionId);
        if (exception != null) {
            // 根据单据类型查询关联的单据信息
            if ("declaration".equals(exception.getDocumentType())) {
                FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(exception.getDocumentId());
                exception.setDeclaration(declaration);
            } else if ("invoice".equals(exception.getDocumentType())) {
                FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(exception.getDocumentId());
                exception.setInvoice(invoice);
            }
        }
        return exception;
    }

    /**
     * 查询配单异常记录列表
     *
     * @param fiscaleRefundException 配单异常记录
     * @return 配单异常记录
     */
    @Override
    public List<FiscaleRefundException> selectFiscaleRefundExceptionList(FiscaleRefundException fiscaleRefundException)
    {
        List<FiscaleRefundException> list = fiscaleRefundExceptionMapper.selectFiscaleRefundExceptionList(fiscaleRefundException);
        for (FiscaleRefundException exception : list) {
            // 根据单据类型查询关联的单据信息
            if ("declaration".equals(exception.getDocumentType())) {
                FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(exception.getDocumentId());
                exception.setDeclaration(declaration);
            } else if ("invoice".equals(exception.getDocumentType())) {
                FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(exception.getDocumentId());
                exception.setInvoice(invoice);
            }
        }
        return list;
    }

    /**
     * 新增配单异常记录
     *
     * @param fiscaleRefundException 配单异常记录
     * @return 结果
     */
    @Override
    public int insertFiscaleRefundException(FiscaleRefundException fiscaleRefundException)
    {
        return fiscaleRefundExceptionMapper.insertFiscaleRefundException(fiscaleRefundException);
    }

    /**
     * 修改配单异常记录
     *
     * @param fiscaleRefundException 配单异常记录
     * @return 结果
     */
    @Override
    public int updateFiscaleRefundException(FiscaleRefundException fiscaleRefundException)
    {
        return fiscaleRefundExceptionMapper.updateFiscaleRefundException(fiscaleRefundException);
    }

    /**
     * 批量删除配单异常记录
     *
     * @param exceptionIds 需要删除的配单异常记录ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleRefundExceptionByIds(Long[] exceptionIds)
    {
        return fiscaleRefundExceptionMapper.deleteFiscaleRefundExceptionByIds(exceptionIds);
    }

    /**
     * 删除配单异常记录信息
     *
     * @param exceptionId 配单异常记录ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleRefundExceptionById(Long exceptionId)
    {
        return fiscaleRefundExceptionMapper.deleteFiscaleRefundExceptionById(exceptionId);
    }

    /**
     * 标记为异常
     *
     * @param documentType 单据类型
     * @param documentId 单据ID
     * @param exceptionReason 异常原因
     * @param remark 备注
     * @return 结果
     */
    @Override
    public int markAsException(String documentType, Long documentId, String exceptionReason, String remark)
    {
        // 检查是否已存在异常记录
        FiscaleRefundException existing = fiscaleRefundExceptionMapper.selectFiscaleRefundExceptionByDocumentTypeAndId(documentType, documentId);
        if (existing != null) {
            // 更新现有异常记录
            existing.setExceptionReason(exceptionReason);
            existing.setRemark(remark);
            existing.setStatus("pending");
            return fiscaleRefundExceptionMapper.updateFiscaleRefundException(existing);
        } else {
            // 创建新的异常记录
            FiscaleRefundException exception = new FiscaleRefundException();
            exception.setDocumentType(documentType);
            exception.setDocumentId(documentId);
            exception.setExceptionReason(exceptionReason);
            exception.setRemark(remark);
            exception.setStatus("pending");
            return fiscaleRefundExceptionMapper.insertFiscaleRefundException(exception);
        }
    }
}
